package com.example.demofdfs.example.link;

import java.util.Iterator;
import java.util.UUID;

public class DoubleLinkedTest2 {

	public static void main(String[] args) {
//		new DoubleLinkedTest2().test();
	}
	
	public void test() {
		Links<String> links = new DoubleLinked<String>();
		Runnable add = new Add(links );
		Runnable remove = new Remove(links);
		
		Thread t1 = new Thread(add);
		Thread t2 = new Thread(remove);
		
		t1.start();
		t2.start();
		
		while(true) {
			sleep(1000);
//			System.out.println(links);
			Iterator<String> it = links.iterator();
			while(it.hasNext())
				it.next();
//				System.out.println(it.next());
			System.out.println("x");
		}
		
	}
	
	void sleep(long l) {
		try {
			Thread.sleep(l);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private class Add implements Runnable {

		Links<String> links;
		
		public Add(Links<String> links) {
			this.links = links;
		}
		
		@Override
		public void run() {
			while(true) {
				sleep(500);
				links.add("node-" + UUID.randomUUID().toString());
			}
		}
		
	}
	private class Remove implements Runnable {
		
		Links<String> links;
		
		public Remove(Links<String> links) {
			this.links = links;
		}
		
		@Override
		public void run() {
			while(true) {
				sleep(500);
				if (links.size() > 0) {
					System.out.println(links.remove(0));
				}
			}
		}
		
	}

}
